import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:share_fit/app/controllers/base/base_sports_controller.dart';
import 'package:share_fit/app/routes/sports_routes.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';
import 'sports_state.dart';

class SportsLogic extends BaseSportsController implements DeviceChangeListener {
  final SportsState state = SportsState();

  @override
  String getRemotePath() {
    return UTEPedConst.pathSports;
  }

  @override
  void onInit() {
    UTEPedDevice.instance.addListener(this);
    super.onInit();
  }

  @override
  void initState() {}

  @override
  void onReady() {
    super.onReady();
    uTEPedSports.getDeviceOperatorState();
    // 请求定位权限
    uTEPedSports.checkLocationPermission();
  }

  @override
  void onRemoteEvent(event, method) {}

  startExercise() {
    uTEPedSports.start();
  }

  pauseExercise() {
    uTEPedSports.pause();
  }

  continueExercise() {
    uTEPedSports.resume();
  }

  goSportsCategoryPage(String route) {
    // UTEPedDevice.instance.isConnected().then((value) {
    //   if (value) {
        Get.toNamed(route)?.then((value) => state.selectedExerciseType = value);
      // } else {
      //   showTips(LocaleKeys.info.ts, LocaleKeys.sports_device_not_connected_text.ts);
      // }
    // });
  }

  @override
  void onDeviceChangeEvent(event) {
    String action = event[UTEPedConst.keyAction];
    if (action == UTEPedConst.actionConnect) {
      queryWorkOutManageInfo();
    }
  }

  onMapCreated(GoogleMapController controller) {
    // 重新获取当前定位
    uTEPedSports.getCurrentLocation().then((pos) {
      controller.moveCamera(CameraUpdate.newLatLng(LatLng(pos.latitude, pos.longitude)));
    });
  }

  goMovementPage() {
    // UTEPedDevice.instance.isConnected().then((value) {
    //   if (value) {
        Get.toNamed(SportsRoutes.movementPage);
    //   } else {
    //     showTips(LocaleKeys.info.ts, LocaleKeys.sports_device_not_connected_text.ts);
    //   }
    // });
  }
}
